package com.finapps.stopspend;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;

import com.finapps.stopspend.dao.ObjectiusDAO;
import com.finapps.stopspend.entidades.BarraSuperior;
import com.finapps.stopspend.entidades.ObjetivoEntity;
import com.finapps.stopspend.entidades.SituacioObjectiu.ColorSituacio;
import com.finapps.stopspend.util.CalendarUtils;

public class AnadirNuevoActivity extends ActivityConMenu {

	private static final String TAG = "STOP";
	
	private Spinner tit;
	private EditText des;
	private EditText pre;
	private DatePicker dat;
	private String titu;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
        setContentView(R.layout.anadirnuevo);
        
        BarraSuperior barra = (BarraSuperior) findViewById (R.id.barraSuperior1);
        
        barra.getBtn1().setVisibility(View.INVISIBLE);

        tit = (Spinner) findViewById(R.id.nuevoTit);
        des = (EditText) findViewById(R.id.nuevoDescrip);
        pre = (EditText) findViewById(R.id.nuevoPrecio);
        dat = (DatePicker) findViewById(R.id.datePPicker);
        
        ArrayAdapter<String> adaptador =
                new ArrayAdapter<String>(this,
                    android.R.layout.simple_dropdown_item_1line, getResources().getStringArray(R.array.Tipos));
        
        tit.setAdapter(adaptador);
        tit.setOnItemSelectedListener(
                new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent,
                    android.view.View v, int position, long id) {
                        titu =  getResources().getStringArray(R.array.Tipos)[position];
                }
         
                public void onNothingSelected(AdapterView<?> parent) {
                    
                }
        });
        
        
        Button aceptar = (Button) findViewById(R.id.btnAceptar);
        Button cancelar = (Button) findViewById(R.id.btnCancelar);
        
        cancelar.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v)
        	{
        		setResult(Activity.RESULT_CANCELED);
        		finish();
        	}
        });
        
        aceptar.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v)
        	{
//        			String titS = tit.getAdapter().getItem(position)
        			String desS = des.getText().toString().trim();
        			int preS = Integer.parseInt(pre.getText().toString().trim());
        			
        			CalendarUtils cal = new CalendarUtils();
        			
        			String strData = Integer.toString(dat.getYear())+Integer.toString(dat.getMonth())+Integer.toString(dat.getDayOfMonth());
        			
        			ObjetivoEntity obj = new ObjetivoEntity(null, titu, desS, preS, preS);
        			Log.i("STOP", obj.getTitulo());
        			obj.setData(cal.data2Calendar(Integer.toString(dat.getDayOfMonth()),Integer.toString(dat.getMonth()),Integer.toString(dat.getYear())));
        			obj.setColorSituacio(ColorSituacio.VERD);
        			ObjectiusDAO oDao = new ObjectiusDAO();
        			oDao.saveObjectiu(obj);
        		setResult(Activity.RESULT_OK);
        		finish();
        	}
        });
	}
	
	public boolean mayorQueZero(EditText txt)
	{
		if (txt.getText().toString().trim().length() > 0)
			return true;
		else
			return false;
	}
	
}
